package com.edu.compile.template.ftl;

import com.edu.compile.ParserHolder;
import com.edu.compile.template.AbstractNodeBlockParse;
import com.edu.compile.template.NodeTemplateParse;
import com.edu.compile.template.TemplatePolymerization;
import com.edu.compile.template.buffer.Buffer;
import com.edu.model.tree.v2.TreeNode;

import java.util.Iterator;
import java.util.List;

/**
 * @author yanzhao
 * @date 2021/10/26 14:56
 * @since 1.0.0
 */
public class BranchNodeBlockParse extends AbstractNodeBlockParse {

    public BranchNodeBlockParse(String nodeId, String nodeType, String nodeName, String parentNodeId) {
        super(nodeId, nodeType, nodeName, parentNodeId);
    }

    @Override
    public String parseMethodCall(TemplatePolymerization handler, TreeNode<NodeTemplateParse> treeNode, Buffer buffer) {
        List<TreeNode<NodeTemplateParse>> childrenNode = treeNode.getChildrenNode();
        Iterator<TreeNode<NodeTemplateParse>> iterator = childrenNode.iterator();

        Buffer thisBuffer = null;
        while (iterator.hasNext()) {
            thisBuffer = new Buffer();
            thisBuffer.setParentBuffer(buffer);
            TreeNode<NodeTemplateParse> next = iterator.next();
            NodeTemplateParse data = next.getData();
            data.parse(handler, next, thisBuffer);
            this.addCall(thisBuffer.toString());
        }
        return null;
    }

    @Override
    public void postParseMethodBody(TemplatePolymerization handler, Buffer buffer) {
        buffer.append(ParserHolder.templateParser.parse(this));
    }

}
